package com.byxy.computer.student.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.byxy.computer.student.dao.StudentDao;
import com.byxy.computer.student.entity.Student;
import com.byxy.computer.student.json.AjaxEasyUIPage;

/**
 * @author Daniel
 * @E-Mail iva0512@gmail.com
 * @Date 2016年9月25日
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentDao studentDao;

	@Override
	public Student save(Student std) {
		return studentDao.save(std);
	}

	@Override
	public List<Student> listAll() {
		return studentDao.findAll();
	}

	@Override
	public Student get(Student std) {
		if (std != null)
			return studentDao.findOne(std.getUid());
		return null;
	}

	@Override
	public List<Student> save(List<Student> std) {
		return studentDao.save(std);
	}

	@Override
	public void deleteAll() {
		studentDao.deleteAllInBatch();
	}

	@Override
	public void sayGood(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			if (s.getGood() == null)
				s.setGood(0);
			if (s.getStats() != null)
				s.setGood(s.getGood().intValue() + 5);
			studentDao.save(s);
		}
	}

	@Override
	public void sayBad(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			if (s.getGood() == null)
				s.setGood(0);
			if (s.getStats() != null)
				s.setGood(s.getGood().intValue() - 5);
			studentDao.save(s);
		}
	}

	@Override
	public void resetLesson(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			s.setGood(0);
			s.setStats(null);
			s.setComputername(null);
			s.setFilesize(0);
			studentDao.save(s);
		}
	}

	@Override
	public Student sign(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			if (!"C".equals(s.getStats()))
				s.setStats("√");
			s.setComputername(std.getComputername());
			return studentDao.save(s);
		}
		return null;
	}

	@Override
	public Student upload(Student std, String filepath, int size) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			s.setHomework(filepath);
			s.setFilesize(size / 1024);
			return studentDao.save(s);
		}
		return null;
	}

	@Override
	public void playTruant(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null) {
			s.setStats("K");
			studentDao.save(s);
		}
	}

	@Override
	public void beLate(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null) {
			s.setStats("C");
			studentDao.save(s);
		}

	}

	@Override
	public void holiday(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null && (s.getStats() == null || "".equals(s.getStats()))) {
			s.setStats("S");
			studentDao.save(s);
		}
	}

	@Override
	public void sick(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null && (s.getStats() == null || "".equals(s.getStats()))) {
			s.setStats("B");
			studentDao.save(s);
		}

	}

	@Override
	public List<Student> getList(int page, int size) {
		List<Student> list = new ArrayList<Student>();
		
//		System.out.println("page:" + page + "size:" + size);
		for (int i = 0; i < studentDao.findAll().size(); i++) {
			if (i >= (page - 1) * size) {
				list.add(studentDao.findAll().get(i));
			}
			if (list.size() >= size)
				break;
		}
		return list;
	}

	
	
	@Override
	public AjaxEasyUIPage getJson(int page, int size) {
		AjaxEasyUIPage ajaxEasyUIPage = new AjaxEasyUIPage();
		Sort sort = new Sort(Direction.ASC, "uid");
		Page<Student> pages = studentDao.findAll(new PageRequest(page - 1, size, sort));

//		List<Student>students=studentDao.findAll();
//		int count=0;
//		for (Student s : students) {
//			if (s.getStats() != null && !"K".equals(s.getStats()) && !"S".equals(s.getStats())
//					&& !"B".equals(s.getStats())) {
//				count++;
//			}
//		}
//		ajaxEasyUIPage.setCount(count);
		
		ajaxEasyUIPage.setTotal(pages.getTotalElements());
		ajaxEasyUIPage.setRows(pages.getContent());
		
		return ajaxEasyUIPage;
	}

	@Override
	public AjaxEasyUIPage getJson(int page, int size, String sortColumn, String order) {
		Sort sort;
		AjaxEasyUIPage ajaxEasyUIPage = new AjaxEasyUIPage();
		if (sortColumn == null) {
			sort = new Sort(Direction.ASC, "uid");
		} else {
			if (order == null) {
				sort = new Sort(Direction.ASC, sortColumn);
			} else {
				if (order.equalsIgnoreCase("asc"))
					sort = new Sort(Direction.ASC, sortColumn);
				else
					sort = new Sort(Direction.DESC, sortColumn);
			}
		}
		Page<Student> pages = studentDao.findAll(new PageRequest(page - 1, size, sort));
		
//		List<Student>students=studentDao.findAll();
//		int count=0;
//		for (Student s : students) {
//			if (s.getStats() != null && !"K".equals(s.getStats()) && !"S".equals(s.getStats())
//					&& !"B".equals(s.getStats())) {
//				count++;
//			}
//		}
//		ajaxEasyUIPage.setCount(count);
		
		ajaxEasyUIPage.setTotal(pages.getTotalElements());
		ajaxEasyUIPage.setRows(pages.getContent());
		
		return ajaxEasyUIPage;
	}

	@Override
	public int getArrive() {
		return studentDao.countArrive();
	}

	@Override
	public int getHomeWork() {
		// TODO Auto-generated method stub
		return studentDao.countHomeWork();
	}

	@Override
	public Student getByUid(String uid) {
		// TODO Auto-generated method stub
		return studentDao.findOne(uid);
	}
	
	
	
}